/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AltaCliente.java
 *
 * Created on 19-jun-2011, 0:19:16
 */

package guiAdministracion;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;
import logica.Fabrica;
import logica.IAdministracion;
import util.datatypes.DTCliente;

/**
 *
 * @author Ernesto
 */
public class AltaCliente extends javax.swing.JFrame {

    private Calendar c;
    private Date d;

    public AltaCliente() {
        initComponents();

        c= Calendar.getInstance();
        d= c.getTime();

        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
      
        FechaN.setText(sdf.format(d));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblTitulo = new javax.swing.JLabel();
        btnAgregar = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        txtCi = new javax.swing.JTextField();
        txtNombre = new javax.swing.JTextField();
        FechaN = new javax.swing.JFormattedTextField();

        setTitle("Administracion de Clientes");
        setMinimumSize(new java.awt.Dimension(375, 200));

        lblTitulo.setText("ALTA DE CLIENTE");
        getContentPane().add(lblTitulo, java.awt.BorderLayout.NORTH);

        btnAgregar.setText("Agregar");
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });
        getContentPane().add(btnAgregar, java.awt.BorderLayout.PAGE_END);

        jPanel4.setLayout(null);

        jPanel1.setLayout(new java.awt.GridLayout(3, 1));

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("Ci:");
        jPanel1.add(jLabel1);

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Nombre:");
        jPanel1.add(jLabel2);

        jLabel3.setText("Fecha de Nacimiento:");
        jPanel1.add(jLabel3);

        jPanel3.add(jPanel1);

        jPanel2.setLayout(new java.awt.GridLayout(3, 1));

        txtCi.setColumns(20);
        jPanel2.add(txtCi);
        jPanel2.add(txtNombre);

        FechaN.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd-MM-yyyy"))));
        jPanel2.add(FechaN);

        jPanel3.add(jPanel2);

        jPanel4.add(jPanel3);
        jPanel3.setBounds(0, 0, 370, 67);

        getContentPane().add(jPanel4, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
        try {
            IAdministracion i = Fabrica.getIAdministracion();

            int ci = Integer.parseInt(txtCi.getText());
            String nombre = txtNombre.getText();
            
            SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
            Date fechaNacimiento = sdf.parse(FechaN.getText());

            if(nombre.equals("")){
                JOptionPane.showMessageDialog(null, "Debe ingresar el nombre");

            }else{
                int resultado= i.crearCliente(new DTCliente(ci, nombre, fechaNacimiento));

                if(resultado == 1){
                    txtCi.setText("");
                    txtNombre.setText("");

                    FechaN.setText(sdf.format(d));

                }else if(resultado == -1){

                    JOptionPane.showMessageDialog(null, "No se pudo agregar. El cliente ya estaba registrado");
                }else if(resultado == -2){

                    JOptionPane.showMessageDialog(null, "No se pudo agregar. Se produjo una excepcion");
                }else{

                    JOptionPane.showMessageDialog(null, "Error al agregar el cliente");
                }
            }
        } catch (ParseException pe) {

            JOptionPane.showMessageDialog(null, "Error al convertir la fecha");
        } catch (NumberFormatException nfe) {

            JOptionPane.showMessageDialog(null, "La cedula debe ser un numero");
            
        }
    }//GEN-LAST:event_btnAgregarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new AltaCliente().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFormattedTextField FechaN;
    private javax.swing.JButton btnAgregar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JLabel lblTitulo;
    private javax.swing.JTextField txtCi;
    private javax.swing.JTextField txtNombre;
    // End of variables declaration//GEN-END:variables

}
